package org.jradweb.core.taglib;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;

import org.jradweb.core.web.UrlMapResolver;
/**
 * e.g. Tag <b>&lt;j:href controller="user" value="list"/&gt;</b> will print "contextPath/user/list.html" to page.
 * <br/>
 * <br/>Created On Apr 21, 2008
 * @author <a href="mailto:wagzhi@gmail.com">Paul Wang</a>
 * @version $Id: UriTag.java 149 2008-10-10 03:10:10Z wagzhi $
 */
public class UriTag extends BaseTag {
	public static final String URI_PARAMETERS="_jradweb_uri_tag_uri_paramters";
	public static final String URI_QURY_PARAMETERS="_jradweb_uri_query_tag_uri_paramters";
	public static final String PAGE_TYPE_JS="js";
	public static final String PAGE_TYPE_HTML="HTML";
	
	private UrlMapResolver urlMapResolver;
	@Override
	public int doStartTag() throws JspException {
		int value= super.doStartTag();
		Map maps=this.appCtx.getBeansOfType(UrlMapResolver.class);
		if(!maps.isEmpty()){
			 this.urlMapResolver=(UrlMapResolver)maps.values().iterator().next();
		}
		
		return value;
	}

	private String filenameExtension;
	private String controller;
	private String action;
	private String type=PAGE_TYPE_HTML;
	private Object oid;

	public Object getOid() {
		return oid;
	}
	public void setOid(Object oid) {
		this.oid = oid;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getController() {
		return controller;
	}

	public void setController(String controller) {
		this.controller = controller;
	}

	@Override
	public int doEndTag() throws JspException {
		HttpServletRequest req = (HttpServletRequest) pageContext.getRequest();
		String contextPath = req.getContextPath();
		String url = this.getUri(contextPath);
		try {
			this.pageContext.getOut().write(url);
		} catch (IOException e) {
			throw new JspException(e);
		}finally{
			pageContext.removeAttribute(URI_PARAMETERS);
			pageContext.removeAttribute(URI_QURY_PARAMETERS);
		}
		return super.doEndTag();

	}

	protected String getUri(String contextPath) {
		Map<String,Object> params=(Map<String,Object>)pageContext.getAttribute(URI_PARAMETERS);
		Map<String,Object> urlParams=new HashMap<String,Object>();
		if(params!=null){
			urlParams.putAll(params);
		}
		if(controller!=null){
			urlParams.put("_controller_", controller);
		}else{
			urlParams.put("_controller_", this.pageContext.getRequest().getAttribute("controller"));
		}
		urlParams.put("_action_", action);
		if(this.id!=null){
			urlParams.put("id", id);
		}
		int iPageType=UrlMapResolver.PAGE_TYPE_HTML;
		if(PAGE_TYPE_JS.equals(this.getType())){
			iPageType=UrlMapResolver.PAGE_TYPE_JS;
		}
		String uri= this.urlMapResolver.getUri(contextPath,urlParams,iPageType);
		
		Map<String,Object> queryParams =(Map<String,Object>)pageContext.getAttribute(URI_QURY_PARAMETERS);
		if(queryParams!=null){
			for(String key:queryParams.keySet()){
				try {
					String value=queryParams.get(key).toString();
					value=java.net.URLEncoder.encode(value,"UTF-8");
					if(uri.indexOf("?")>0){
						uri=uri+"&"+key+"="+value;
					}
					else{
						uri=uri+"?"+key+"="+value;
					}
				} catch (UnsupportedEncodingException e) {
					throw new RuntimeException(e);
				}
			}
		}
		
		return uri;
	}

	public String getFilenameExtension() {
		return filenameExtension;
	}

	public void setFilenameExtension(String filenameExtension) {
		this.filenameExtension = filenameExtension;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = (type==null||type.length()==0)?PAGE_TYPE_HTML:type;
	}

}
